<template>
  <div class="info" v-loading="loading">
    <div class="card_con search_data mb15">
      <el-row class="mb15">
        <el-col :span="6" class="pr40">
          <div class="fz13 mb10">分类名称：</div>
          <el-input
            clearable
            v-model="classification_name"
            placeholder="请输入分类名称"
          ></el-input>
        </el-col>
        <el-col :span="6" class="pr40">
          <div class="fz13 mb10">分类状态：</div>
          <el-select clearable v-model="shopStateId" placeholder="请选择">
            <el-option
              v-for="item in shopStateArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <div class="fz13 mb10">&nbsp;</div>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="card_con">
      <div class="row mb15">
        <span class="l" @click="handleAdd">
          <i class="el-icon-circle-plus-outline"></i>添加分类
        </span>
      </div>
      <el-table
        highlight-current-row
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
        :header-cell-style="{
          'text-align': 'center',
        }"
        :cell-style="{ 'text-align': 'center' }"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <!-- <el-table-column type="selection" min-width="5%"> </el-table-column> -->
        <el-table-column prop="id" type="" label="ID" min-width="15%">
        </el-table-column>
        <el-table-column prop="name" label="分类名称" min-width="20%">
        </el-table-column>
        <el-table-column label="分类图标" min-width="15%">
          <template slot-scope="scope">
            <div class="slt">
              <el-popover placement="right" width="200" trigger="hover">
                <img
                  v-if="scope.row.icon"
                  class="tbImg1"
                  :src="scope.row.icon.src"
                  alt=""
                />
                <img
                  v-if="scope.row.icon"
                  class="tbImg"
                  slot="reference"
                  :src="scope.row.icon.src"
                  alt=""
                />
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" min-width="15%">
        </el-table-column>
        <el-table-column prop="status" label="状态" min-width="15%">
          <template slot-scope="scope">
            <el-switch
              @change="changeSwitch(scope.row)"
              active-text="禁用"
              inactive-text="启用"
              :active-value="1"
              :inactive-value="0"
              class="tablescope"
              v-model="scope.row.status"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="20%">
          <template slot-scope="scope">
            <div class="caozuo">
              <div class="handleEdit hand_cs" @click="handleEdit(scope.row)">
                修改
              </div>
              <div
                class="handleDelete hand_cs"
                @click="handleDelete(scope.row)"
              >
                删除
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination :page="page" @onCurrentChange="handleCurrentChange" />
      </div>
      <el-dialog
        :title="title"
        :close-on-click-modal="false"
        :visible.sync="Dialog"
        width="50%"
      >
        <Dialog
          v-if="Dialog"
          :rowData="rowData"
          @handleClose="handleClose"
        ></Dialog>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination.vue";
import { shopCategory_list, cateStatus } from "@/api/mall_management";
import Dialog from "./add_classification_dialog.vue";
export default {
  components: { Pagination, Dialog },
  data() {
    return {
      classification_name: "", //分类名称
      shopStateArr: [
        // { id: "0", name: "全部" },
        { id: "1", name: "启用" },
        { id: "2", name: "禁用" },
      ],
      shopStateId: "",
      page: {
        total: 0,
        pageNum: 1, // 当前页数
        pageSize: 100, // 每页的条数
      },
      Dialog: false,
      tableData: [
        {
          id: 1,
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          status: true,
          logo: require("@/assets/1.png"),
        },
        {
          id: 2,
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
          status: true,
          logo: require("@/assets/1.png"),
        },
        {
          id: 3,
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
          status: true,
          logo: require("@/assets/1.png"),
          children: [
            {
              id: 31,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
              status: true,
              logo: require("@/assets/1.png"),
            },
            {
              id: 32,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
              status: true,
              logo: require("@/assets/1.png"),
            },
          ],
        },
        {
          id: 4,
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
          status: true,
          logo: require("@/assets/1.png"),
          children: [
            {
              id: 33,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
              status: true,
              logo: require("@/assets/1.png"),
            },
            {
              id: 34,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
              status: true,
              logo: require("@/assets/1.png"),
            },
          ],
        },
      ],
      title: "",
      rowData: {},
      loading: true,
    };
  },
  methods: {
    //商品列表
    async shopCategory_list() {
      const { data, err } = await shopCategory_list({
        fl_name: this.classification_name,
        shopState: this.shopStateId,
      });
      if (err === 0) {
        this.loading = false;
        this.tableData = data.data;
        this.page.total = data.total;
      }
    },
    //开启禁用删除
    async cateStatus(id, st) {
      const { data, err } = await cateStatus({
        id,
        st,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
      } else {
        this.$message({
          showClose: true,
          message: "操作失败",
          type: "error",
        });
      }
      this.shopCategory_list();
    },
    //开关操作
    changeSwitch(row) {
      //1通过  2禁用
      if (row.status === 0) {
        //禁用
        console.log(row.id);
        this.cateStatus(row.id, 2);
      } else {
        this.cateStatus(row.id, 1);
      }
    },
    //查询
    handleSearch() {
      this.loading = true;
      this.shopCategory_list();
    },
    //新增
    handleAdd() {
      this.rowData = {};
      this.Dialog = true;
      this.title = "添加分类";
    },
    //编辑
    handleEdit(row) {
      console.log(row);
      this.rowData = row;
      console.log(this.rowData);
      this.Dialog = true;
      this.title = "编辑";
    },
    //删除
    handleDelete(row) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.cateStatus(row.id, "-1");
        })
        .catch(() => {});
    },
    // 当前页: ${val}
    handleCurrentChange(val) {
      this.page.pageNum = val;
    },
    handleClose() {
      this.Dialog = false;
      this.shopCategory_list();
    },
  },
  created() {
    this.shopCategory_list();
  },
};
</script>

<style lang="scss" scoped>
@import "@/style/index.scss";

.info {
  .title {
    font-size: 16px;
    margin-bottom: 13px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  padding-top: 10px;
}
.el-select {
  width: 100%;
}

.tb {
  background-color: #fff;
  padding: 20px;
  box-sizing: border-box;
}
.row {
  display: flex;
  border-radius: 2px;
  .l {
    padding: 8px 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #ccc;
    font-size: 13px;
    i {
      margin-right: 6px;
      font-size: 13px;
    }
    &:hover {
      cursor: pointer;
    }
  }
}
.editDialog {
  //   z-index: 2005 !important;
  /deep/.el-dialog {
    height: 75%;
    .el-dialog__body {
      height: 78%;
      overflow: auto;
    }
  }
}
.handleEdit {
  margin-left: 20px;
}
.handleDelete {
  margin-left: 20px;
}

/deep/.el-table .el-table__cell {
  padding: 10px 0;
}
.search_data {
  /deep/.el-input__inner {
    height: 36px;
    line-height: 36px;
    font-size: 13px;
  }
}
</style>
